﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    [PermissionCheck(false)]
    public partial class SelectBZXKC : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            BindCPX(this.ddlCPX, "请选择");
            ddlCPHXC.Items.Add(new ListItem("请选择", ""));
            this.AspNetPager1.PageSize = GridView1.PageSize;
        }

        protected void ddlCPX_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ddlCPX.SelectedValue))
            {
                ddlCPHXC.Items.Clear();
                ddlCPHXC.Items.Add(new ListItem("请选择", ""));
            }
            else
                BindCPHXC(ddlCPHXC, ddlCPX.SelectedValue, "请选择");
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        private void BindData()
        {
            Dictionary<string,string> dc=new Dictionary<string,string>();
            if (!string.IsNullOrEmpty(ddlCPX.SelectedValue))
                dc.Add("cpxbh", ddlCPX.SelectedValue);
            if (!string.IsNullOrEmpty(ddlCPHXC.SelectedValue))
                dc.Add("cphxcbh", ddlCPX.SelectedValue);
            if (!string.IsNullOrEmpty(tbBH.Text.Trim()))
                dc.Add("bh", tbBH.Text.Trim());
            if (!string.IsNullOrEmpty(tbMC.Text.Trim()))
                dc.Add("mc", tbMC.Text.Trim());
            if (!string.IsNullOrEmpty(ddlLX.SelectedValue))
                dc.Add("lx", ddlLX.SelectedValue);

            using (KCBLL kBLL = new KCBLL())
            {
                int RecordCount = 0;
                GridView1.DataSource = kBLL.GetBZXKC(dc, false, "BH asc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                GridView1.DataBind();
                this.AspNetPager1.RecordCount = RecordCount;
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
            List<int> cols = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
            Utility.MergeCols(GridView1, cols);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}